﻿using NB.MGJ.Common.API;
using NB.MGJ.Common.OSS;
using NB.MGJ.Model;
using NB.MGJ.Model.SystemEntity;
using NB.MGJ.Service;
using NB.MGJ.Service.AntiFake;
using NB.MGJ.Service.Product;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace NB.MGJ.API
{
    // [ApiAuthorizeFilter]
    [ApiHandleError]
    public class ApiMGJCommController : ApiBaseController<Sys_User>
    {
        public ProductBatchService _ProductBatchService = new ProductBatchService();
        [Route("nbapi/comm/file/upload")]
        [HttpPost]
        public IHttpActionResult CommonFileUpload()
        {
            var files = HttpContext.Current.Request.Files;

            if (files.Count == 0)
            {
                return ApiFail("无文件");
            }
            var postedFile = files[0];
            string fileName = System.Guid.NewGuid().ToString() + System.IO.Path.GetFileName(postedFile.FileName);

            if (!string.IsNullOrEmpty(fileName))
            {
                var stream = postedFile.InputStream;
                OssManager.Instance.PutObject(fileName, stream);
                string filePath = OssManager.Instance.GetUrl(fileName).Replace("http://", "https://");

                return ApiSuccess(filePath);
            }
            else
            {
                return ApiSuccess("无文件");
            }

        }


        /// <summary>
        /// 获取当前机构的直接子级机构
        /// </summary>
        /// <param name="enterpriseId"></param>
        /// <returns></returns>
        [Route("nbapi/comm/get/enterprise/direct/sub/level")]
        [HttpGet]
        public IHttpActionResult GetCurrentEnterpriseDirectSubLevel(int enterpriseId)
        {
            return ApiSuccess(new ManagerService().GetDirectSubLevel(enterpriseId));
        }

        #region 扫码记录
        [Route("nbapi/memberInfo/Code/list")]
        [HttpGet]
        public IHttpActionResult CodeList(int pageindex = 1, int pageSize = 10, string unique = null, int? userid = null)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageindex;
            pm.PageSize = pageSize;
            if (string.IsNullOrWhiteSpace(unique) && !userid.HasValue)
            {
                return ApiFail("请求失败");
            }
            if (userid.HasValue)
            {
                pm.StrCondition = "  UserId=" + userid;
            }
            if (!string.IsNullOrWhiteSpace(unique))
            {
                if (string.IsNullOrWhiteSpace(pm.StrCondition))
                {
                    pm.StrCondition = "  UniqueIdentifier=" + "'" + unique + "'";
                }
                else
                {
                    pm.StrCondition += " Or UniqueIdentifier=" + "'" + unique + "'";
                }
            }

            if (string.IsNullOrWhiteSpace(pm.StrCondition))
            {
                pm.StrCondition = " 1=1 ";
            }

            var model = _ProductBatchService.GetCodePageList(ref pm);
            return ApiSuccess(model.PageData);
        }

        #endregion


        #region 统计

        [Route("nbapi/comm/sys/data/statistics")]
        [HttpGet]
        public IHttpActionResult GetSysDataStatistics(int userId, string enterpriseId, string unCode)
        {
            var rs = new CommonService().SystemDataStatistics(userId, enterpriseId, unCode);
            return ApiSuccess(rs);
        }


       

        #endregion

        #region 系统反馈
        //意见反馈
        [Route("nbapi/system/feedback/add")]
        [HttpPost]
        public IHttpActionResult WxFeedbackList(Sys_FeedBack FeedBack)
        {
            FeedBack.FeedbackDate = DateTime.Now;
            return ApiSuccess(new AppraiseListService().AddwxFeedBack(FeedBack));
        }
        #endregion
    }
}
